assert(PandaDKP, "PandaDKP not found!")

------------------------------
--      Are you local?      --
------------------------------
local PandaDKP = PandaDKP
local Messages =
{
   Queue = {},
   Failed = {},
   ItList = {},
}
PandaDKP.Messages = Messages


function Messages:Enable()
end

function Messages:StartTimer()
   if (nil == Messages.Timer) then
      Messages.Timer = PandaDKP:ScheduleRepeatingTimer(function() PandaDKP.Messages:ProcessQueue() end, 1)
   end
end

function Messages:StopTimer()
   if (nil ~= Messages.Timer) then PandaDKP:CancelTimer(Messages.Timer) end
   Messages.Timer = nil
end


function Messages:QueueMessage(sender, message)
   -- Create the message record
   local record = {}
   record.sender = sender
   record.message = message
   
   -- Queue the message
   tinsert(Messages.Queue, record)
end


function Messages:ProcessQueue()
   -- Pop up to five messages off the queue and process them
   for i = 1, 5 do
      -- See if the queue is empty
      if (0 == #Messages.Queue) then break end
      
      -- Pop the first message and process it
      Messages:ProcessMessage(tremove(Messages.Queue, 1))
   end
end


function Messages:ProcessMessage(record)
   -- Strip the command from the message
   local command, rest = PandaDKP:ParseCommand(record.message)
   
   -- Switch the command so that we can parse the parameters
   if ("OPEN" == command) then
      Messages:HandleOpenCommand(record.sender, rest)
   elseif ("CLOSED" == command) then
      PandaDKP.Bidding:BiddingClosed(record.sender)
   elseif ("CANCEL" == command) then
      PandaDKP.Bidding:BiddingCanceled(record.sender)
   elseif ("ACCEPTED" == command) then
      Messages:HandleAcceptedCommand(record.sender, rest)
   elseif ("BID" == command) then
      Messages:HandleBidCommand(record.sender, rest)
   elseif ("UNBID" == command) then
      Messages:HandleUnbidCommand(record.sender, rest)
   else
      -- all the others we can safely ignore
   end
end

      -- OPEN link                  Received when an item is up for bid
      -- CLOSED                     Bids will no longer be accepted
      -- CANCEL                     Bidding was canceled
      -- BID itemId (N/G)           We send this when we place a bid
      -- ACCEPTED itemId (N/G/U)    Response to let us know bid was received or canceled
      -- UNBID itemId               We send this to cancel our bid




function Messages:HandleOpenCommand(sender, rest)
   local _, _, link = strfind(rest, '(.+)')
   if (nil == link) then return end
   
   -- Try the link, if its NULL, then query the server
   local itemId = PandaDKP:ExtractItemID(link)
   local _, testLink = GetItemInfo(itemId)
   if (nil == testLink) then
      GameTooltip:SetHyperlink("item:"..itemId..":0:0:0:0:0:0:0")
   end
   
   PandaDKP.Bidding:BiddingOpen(sender, link)
end

function Messages:HandleAcceptedCommand(sender, rest)
   local _, _, itemId, bid = strfind(rest, '(.+) (%u)')
   if (nil == itemId or nil == bid ) then return end
   PandaDKP.Bidding:BidAccepted(sender, itemId, bid)
end

function Messages:HandleBidCommand(sender, rest)
   local _, _, itemId, state = strfind(rest, '(%d+) (%a)')
   if (nil == itemId or nil == state) then return end
   PandaDKP.Looting:BidReceived(sender, itemId, state)
end

function Messages:HandleUnbidCommand(sender, rest)
   local _, _, itemId = strfind(rest, '(.+)')
   if (nil == itemId) then return end
   PandaDKP.Looting:BidReceived(sender, itemId, 'U')
end